package com.zjwd.project.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.zjwd.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zjwd.common.annotation.Log;
import com.zjwd.common.core.controller.BaseController;
import com.zjwd.common.core.domain.AjaxResult;
import com.zjwd.common.enums.BusinessType;
import com.zjwd.project.domain.ProProject;
import com.zjwd.project.service.IProProjectService;
import com.zjwd.common.utils.poi.ExcelUtils;
import com.zjwd.common.core.page.TableDataInfo;

/**
 * 建设项目Controller
 *
 * @author liquan
 * @date 2024-12-04
 */
@RestController
@RequestMapping("/project/project")
public class ProProjectController extends BaseController {

    @Autowired
    private IProProjectService service;

    /**
     * 查询列表
     */
    @PreAuthorize("@ss.hasPermi('project:project:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProProject model) {
        model.setPlatformCode(getPlatformCode());
        startPage();
        List<ProProject> list = service.selectModelList(model);
        return getDataTable(list);
    }

    /**
     * 导出
     */
    @PreAuthorize("@ss.hasPermi('project:project:export')")
    @Log(title = "建设项目", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ProProject model) {
        List<ProProject> list = service.selectModelList(model);
        ExcelUtils<ProProject> util = new ExcelUtils<ProProject>(ProProject. class);
        return util.exportExcel(list, "建设项目数据");
    }

    /**
     * 获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:project:query')")
    @GetMapping(value = "/{projectId}")
    public AjaxResult getInfo(@PathVariable("projectId") Integer projectId) {
        ProProject project = service.getModelById(projectId);
        if (StringUtils.isNotEmpty(project.getProvince())) {
            String[] citys = new String[3];
            citys[0] = project.getProvince();
            citys[1] = project.getCity();
            citys[2] = project.getCounty();
            project.setCitys(citys);
        }
        return AjaxResult.success(project);
    }

    /**
     * 新增
     */
    @PreAuthorize("@ss.hasPermi('project:project:add')")
    @Log(title = "建设项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProProject model) {
        if (!service.checkProjectCodeUnique(model)) {
            return error("新增项目'" + model.getProjectCode() + "'失败，项目编号已存在");
        }
        return toAjax(service.insert(model));
    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('project:project:edit')")
    @Log(title = "建设项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProProject model) {
        model.setUpdateBy(getUserAccount());
        return toAjax(service.update(model));
    }

    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('project:project:remove')")
    @Log(title = "建设项目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[]ids) {
        return toAjax(service.deleteByIds(ids));
    }
}
